import { resolve } from 'node:path';

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import AutoImport from 'unplugin-auto-import/vite';
import { AntDesignVueResolver, VueUseComponentsResolver } from 'unplugin-vue-components/resolvers';
import components from 'unplugin-vue-components/vite';
import checkerPlugin from 'vite-plugin-checker';
import Unocss from 'unocss/vite';

function pathResolve(dir: string) {
  return resolve(process.cwd(), '.', dir);
}

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    host: true,
    open: false,
  },
  resolve: {
    alias: [
      {
        find: /\/#\//,
        replacement: `${pathResolve('types')}/`,
      },
      {
        find: '@',
        replacement: `${pathResolve('src')}/`,
      },
    ],
    dedupe: ['vue'],
  },

  plugins: [
    vue(),
    vueJsx(),
    Unocss(),
    AutoImport({
      dts: './types/auto-imports.d.ts',
      vueTemplate: true,
      imports: [
        'vue',
        '@vueuse/core',
        {
          from: 'vue',
          imports: ['Ref', 'ComputedRef', 'PropType'],
          type: true,
        },
      ],
    }),
    components({
      dts: './types/components.d.ts',
      resolvers: [AntDesignVueResolver(), VueUseComponentsResolver()],
      extensions: ['vue', 'tsx'],
      deep: true,
    }),
    checkerPlugin({
      eslint: {
        lintCommand: 'eslint \'./src/**/*.{ts,tsx,vue}\'',
        useFlatConfig: true,
      },
      enableBuild: false,
    }),
  ],
});
